#pragma once
#include<string.h>
#include<assert.h>
namespace Wang
{
	template<class T>
	class vector
	{
	public:
		typedef T* iterator;
		~vector()
		{
			delete[]_start;
		}
		size_t size()
		{
			return _finish - _start;
		}
		size_t capacity()
		{
			return _storage - _start;
		}
		iterator begin()
		{
			return _start;
		}
		iterator end()
		{
			return _finish;
		}
		void reserve(size_t n)
		{
			if (n > capacity())
			{
				/*size_t sz;
				iterator start = _start;
				iterator tmp = new T[n];
				if (_start != nullptr)
				{
					memcpy(tmp, _start, sizeof(T) * size());
					delete[] _start;

					sz = size();
				}
				else
					sz = 0;

				_start = tmp;
				_finish = _start + sz;
				_storage = _start + n;*/
				
				iterator tmp = new T[n];
				if (_start)
				{
					memcpy(tmp, _start, sizeof(T) * size());
					delete[]_start;
				}

				_finish = tmp + size();
				_start = tmp;
				_storage = _start + n;
			
			}
		}
		T& operator[](size_t i)
		{
			return _start[i];
		}
		void push_back(const T& val)
		{
			if (_finish == _storage)
			{
				reserve(_storage == 0 ? 4 : 2 * capacity());
			}
			*_finish = val;
			++_finish;
		}

		iterator insert(iterator pos, const T& val)
		{
			if (_finish == _storage)
			{
				int index = pos - _start;
				reserve(_storage == 0 ? 4 : 2 * capacity());
				pos = _start + index;
			}
			for (iterator i = end(); i != pos; --i)
			{
				*i = *(i - 1);
			}
			*pos = val;
			_finish += 1;
			return pos;
		}

		void erase(iterator pos)
		{
			assert(pos >= _start && pos < _finish);
			while (pos < _finish - 1)
			{
				*pos = *(pos + 1);
				++pos;
			}
			_finish -= 1;
		}
	private:
		iterator _start = nullptr;
		iterator _finish = nullptr;
		iterator _storage = nullptr;
	};
}